package view;

import client.TankClient;
import com.fasterxml.jackson.core.JsonProcessingException;
import entity.Direction;
import entity.Position;
import entity.Tank;
import entity.TankActivity;
import server.GpcRequest;
import server.GpcRequestHandler;
import server.netty.GpcDataPackage;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class TankView extends Tank implements TankActivity {

	private static final long serialVersionUID = -2437458497404818925L;
	private Graphics2D g2d = null;
	private List<BulletView> bullet = new ArrayList<>();
	private TankClient client;
	private String entityId;
	private AtomicInteger index = new AtomicInteger(1);
	
	public TankView(TankClient client) {
		this.client = client;
	}
	
	//画一个坦克
	public void draw(Graphics g) {
		drawArc(g, this);
		for(BulletView b:bullet) {
			b.draw(g2d);
		}
	}
	
	public void addBullet(BulletView bulletView) {
		bullet.add(bulletView);
	}
	
	public void destroyBullet(BulletView bulletView) {
		bullet.remove(bulletView);
	}
	
	/**
	 * 3. 圆弧 / 扇形
	 */
	private void drawArc(Graphics g,Tank tank) {
		if(g2d==null)
			g2d = (Graphics2D) g.create();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setColor(Color.PINK);
		// 1. 绘制坦克的身体
		drawTankBody(tank.getCurrentPosition());
		// 2.绘制坦克的炮口
		drawTankGun(tank.getCurrentPosition(), tank.getDirection());
	}

	private void drawTankBody(Position position) {
		g2d.fillArc(position.getX(), position.getY(), Tank.TWidth, Tank.THeight, 0, 360);
	}

	private void drawTankGun(Position position,Direction dir) {
		int gunX = 0;
		int gunY = 0;
		int destX = 0;
		int destY = 0;
		float[] dash = new float[] { Tank.GunLength };
		BasicStroke bs2 = new BasicStroke(
				4,                      // 画笔宽度/线宽
				BasicStroke.CAP_SQUARE,
				BasicStroke.JOIN_MITER,
				10.0f,
				dash,                   // 虚线模式数组
				0.0f
				);
		g2d.setStroke(bs2);
		g2d.setColor(Color.WHITE);
		switch (dir) {
		case LEFT:
			gunX = position.getX()+Tank.TWidth/2+2;
			gunY = position.getY()+Tank.THeight/2;
			destX = gunX-Tank.GunLength;
			destY = gunY;
			setGunPosition(new Position(destX, destY-1));
			break;
		case RIGHT:
			gunX = position.getX()+Tank.TWidth/2-2;
			gunY = position.getY()+Tank.THeight/2;
			destX = gunX+Tank.GunLength;
			destY = gunY;
			setGunPosition(new Position(destX, destY-1));
			break;
		case UP:
			gunX = position.getX()+Tank.TWidth/2;
			gunY = position.getY()+Tank.THeight/2+2;
			destX = gunX;
			destY = gunY-Tank.GunLength;
			setGunPosition(new Position(destX-1, destY));
			break;
		case DOWN:
			gunX = position.getX()+Tank.TWidth/2;
			gunY = position.getY()+Tank.THeight/2-2;
			destX = gunX;
			destY = gunY+Tank.GunLength;
			setGunPosition(new Position(destX-1, destY));
			break;
		default:
			break;
		}
		g2d.drawLine(gunX, gunY, destX, destY);
	}
	
	public void dispose() {
		if(g2d!=null)
			g2d.dispose();
	}
	
	public void shoot() {
		//Bullet bullet = 
	}

	public void start() {
		
	}

	public void dead() {

	}
	
	public void bind(String entityId) {
		this.entityId = entityId;
	}
	public void move() {
		//MoveGenerator.move(this.getCurrentPosition(), this.getDirection(), this.getSpeed(), this.getClass());
		GpcDataPackage message = new GpcDataPackage();
		message.setStatus(10);
		message.setCode(1);
		message.setRouteRequest(buildRequest()
				.setHeader("X_ACTION", "MOVE")
				.setHeader("DIRECTION",this.getDirection().name())
				.setHeader("SPEED", String.valueOf(this.getSpeed().getSpeed()))
				.setHeader("X_POSITION", String.valueOf(this.getCurrentPosition().getX()))
				.setHeader("Y_POSITION", String.valueOf(this.getCurrentPosition().getY())));
		message.setUnique(this.entityId);
		message.setIndex(index.getAndIncrement());
		try {
			message.setLength(message.getRouteRequest().length());
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		client.messageResponse(message);
	}
	
	public GpcRequest buildRequest() {
		return GpcRequestHandler.createRequest(this);
	}
	
	public void initBullet() {

	}

	public void turnLeft() {
		setDirection(Direction.LEFT);
	}
	public void turnRight() {
		setDirection(Direction.RIGHT);
	}
	public void turnDown() {
		setDirection(Direction.DOWN);
	}
	public void turnUp() {
		setDirection(Direction.UP);
	}

}
